package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CullenUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.module.domain.Account;
import com.ruoyi.module.domain.WithdrawalAmount;
import com.ruoyi.module.mapper.AccountMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.FundLendingMapper;
import com.ruoyi.module.domain.FundLending;
import com.ruoyi.module.service.IFundLendingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资金拆借Service业务层处理
 *
 * @author fubojin
 * @date 2025-01-10
 */
@Service
public class FundLendingServiceImpl implements IFundLendingService {
    @Autowired
    private FundLendingMapper fundLendingMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询资金拆借
     *
     * @param id 资金拆借主键
     * @return 资金拆借
     */
    @Override
    public FundLending selectFundLendingById(Long id) {
        return fundLendingMapper.selectFundLendingById(id);
    }

    /**
     * 查询资金拆借列表
     *
     * @param fundLending 资金拆借
     * @return 资金拆借
     */
    @Override
    public List<FundLending> selectFundLendingList(FundLending fundLending) {
        return fundLendingMapper.selectFundLendingList(fundLending);
    }

    /**
     * 新增资金拆借
     *
     * @param fundLending 资金拆借
     * @return 结果
     */
    @Override
    public int insertFundLending(FundLending fundLending) {
        SysUser sysUser = userService.selectUserByPhone(fundLending.getPhone());
        CullenUtils.validateDataThrowException(sysUser == null, "该电话号码对应用户不存在");
        fundLending.setApplyUserId(sysUser.getUserId());
        fundLending.setCreateTime(DateUtils.getNowDate());
        return fundLendingMapper.insertFundLending(fundLending);
    }

    /**
     * 修改资金拆借
     *
     * @param fundLending 资金拆借
     * @return 结果
     */
    @Override
    public int updateFundLending(FundLending fundLending) {
        return fundLendingMapper.updateFundLending(fundLending);
    }

    /**
     * 批量删除资金拆借
     *
     * @param ids 需要删除的资金拆借主键
     * @return 结果
     */
    @Override
    public int deleteFundLendingByIds(Long[] ids) {
        return fundLendingMapper.deleteFundLendingByIds(ids);
    }

    /**
     * 删除资金拆借信息
     *
     * @param id 资金拆借主键
     * @return 结果
     */
    @Override
    public int deleteFundLendingById(Long id) {
        return fundLendingMapper.deleteFundLendingById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int approve(Long id) {
        FundLending fundLending = fundLendingMapper.selectFundLendingById(id);
        fundLending.setRechargeStatus(2);
        fundLending.setReasonTime(DateUtils.getNowDate());
        fundLendingMapper.updateFundLending(fundLending);

        //周转对象个人账户余额处理
        Long userId = fundLending.getUserId();
        Account account = accountMapper.selectAccountByUserId(userId);//查询是否存在余额
        CullenUtils.validateDataThrowException(account == null || account.getAccountBalance().compareTo(fundLending.getPrice()) < 0, "账户余额已不足，无法审核通过");
        account.setAccountBalance(account.getAccountBalance().subtract(fundLending.getPrice()));
        account.setUpdateTime(DateUtils.getNowDate());
        accountMapper.updateAccount(account);

        //周转人个人账户余额处理

        //个人账户余额处理
        Long applyUserId = fundLending.getApplyUserId();
        Account applyAccount = accountMapper.selectAccountByUserId(userId);//查询是否存在余额
        if (applyAccount == null){
            Account accountAdd = new Account();
            accountAdd.setUserId(applyUserId);
            accountAdd.setAccountBalance(fundLending.getPrice());
            accountAdd.setType(1);
            accountAdd.setCreateTime(DateUtils.getNowDate());
            accountMapper.insertAccount(accountAdd);
        }else {
            applyAccount.setAccountBalance(applyAccount.getAccountBalance().add(fundLending.getPrice()));
            applyAccount.setUpdateTime(DateUtils.getNowDate());
            accountMapper.updateAccount(applyAccount);
        }

        return 1;
    }

    @Override
    public int reject(Long id, String reasonReason) {
        FundLending fundLending = fundLendingMapper.selectFundLendingById(id);
        fundLending.setRechargeStatus(3);
        fundLending.setReasonTime(DateUtils.getNowDate());
        fundLending.setReasonReason(reasonReason);
        fundLendingMapper.updateFundLending(fundLending);
        return 1;
    }

    @Override
    public int cancel(Long id, String reasonReason) {
        FundLending fundLending = fundLendingMapper.selectFundLendingById(id);
        fundLending.setRechargeStatus(4);
        fundLending.setReasonReason(reasonReason);
        fundLending.setUpdateTime(DateUtils.getNowDate());
        fundLendingMapper.updateFundLending(fundLending);
        return 1;
    }

}
